<?php
namespace Biz\model;

use Doraemon\pockets\datebase\BaseDbModel;

class ShopAdorn extends BaseDbModel
{

	const _CONFIG_FILE_ = 'db_shop';

	const _CONFIG_SELECT_ = 'db_shop';

	const _DATABASE_ = 'shop';

	const _TABLE_NAME = 'shop_adorn';


	const STATUS_NORMAL = 1;
	const STATUS_DEL = 0;


	const LINK_TYPE_NORLINK = 0;

	const LINK_TYPE_MULTIPIC = 2;
	const LINK_TYPE_SIGLEPIC = 1;



	public static function createPicGrpId($uid)
	{

		return $uid . "_" . base_convert(time(), 10, 16);

	}

	public static function isOwnerPicGrpId($uid, $picGrpId)
	{
		return explode("_", $picGrpId)[0] == $uid;
	}

	public static function isPicGrpId($picGrpId)
	{
		return strpos($picGrpId, '_') !== false;
	}


	public static function addMultiPic(array $banner_infos)
	{
		$banner_infos = array_map(function ($banner) {
			return [
				'pic_group_id' => $banner['pic_group_id'],
				'uid' => $banner['uid'],
				'pic_id' => $banner['pic_id'],
				'link_target' => $banner['link_target'],
				'banner_order' => $banner['banner_order'],
				'link_type' => $banner['link_type'],
				'order' => $banner['order'],
				'addtime' => date('Y-m-d H:i:s'),
				'status' => self::STATUS_NORMAL,
			];
		}, $banner_infos);


		return self::insert($banner_infos);

	}

	public static function updateData(array $data, array $where)
	{

		if (!isset($where['status'])) {
			$where['status'] = self::STATUS_NORMAL;
		}

		if (!isset($data['edittime'])) {
			$data['edittime'] = date("Y-m-d H:i:s");
		}

		return self::update($data, $where);

	}

	public static function getInfo($uid, array $filter = [])
	{

		$map = [];
		$map['uid'] = $uid;
		$map['status'] = self::STATUS_NORMAL;
		foreach ($filter as $field => $condition) {
			$map[$field] = $condition;
		}

		$map['ORDER'] = [
			'banner_order' => 'ASC',
			'order' => 'ASC'
		];

		return self::select('*', $map);

	}

	public static function  addSinglePic(array $banner_info)
	{

		self::insert([
			'uid' => $banner_info['uid'],
			'pic_id' => $banner_info['pic_id'],
			'link_target' => $banner_info['link_target'],
			'banner_order' => $banner_info['banner_order'],
			'link_type' => $banner_info['link_type'],
			'addtime' => date('Y-m-d H:i:s'),
			'status' => self::STATUS_NORMAL
		]);

		return self::id();
	}

	public static function getGoodsIds($id)
	{

		$linkTarget = self::get('link_target', ['id' => $id, 'status' => self::STATUS_NORMAL]);

		return explode(',', $linkTarget);

	}


}